package com.titleevaluation.service;

import com.titleevaluation.entity.Application;
import com.titleevaluation.entity.ContinuingEducation;
import com.titleevaluation.repository.ApplicationRepository;
import com.titleevaluation.repository.ContinuingEducationRepository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class ContinuingEducationService {

    private final ContinuingEducationRepository continuingEducationRepository;
    private final ApplicationRepository applicationRepository;

    public ContinuingEducationService(ContinuingEducationRepository continuingEducationRepository, ApplicationRepository applicationRepository) {
        this.continuingEducationRepository = continuingEducationRepository;
        this.applicationRepository = applicationRepository;
    }

    public ContinuingEducation createContinuingEducation(Long applicationId, ContinuingEducation continuingEducation) {
        Application application = applicationRepository.findById(applicationId)
                .orElseThrow(() -> new RuntimeException("Application not found"));
        continuingEducation.setApplication(application);
        continuingEducation.setCreatedAt(LocalDateTime.now());
        continuingEducation.setUpdatedAt(LocalDateTime.now());
        return continuingEducationRepository.save(continuingEducation);
    }

    public List<ContinuingEducation> getContinuingEducationByApplicationId(Long applicationId) {
        return continuingEducationRepository.findByApplicationId(applicationId);
    }

    public Optional<ContinuingEducation> getContinuingEducationById(Long id) {
        return continuingEducationRepository.findById(id);
    }

    public ContinuingEducation updateContinuingEducation(Long id, ContinuingEducation updatedContinuingEducation) {
        ContinuingEducation existingContinuingEducation = continuingEducationRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("ContinuingEducation not found"));

        existingContinuingEducation.setEducationType(updatedContinuingEducation.getEducationType());
        existingContinuingEducation.setEducationName(updatedContinuingEducation.getEducationName());
        existingContinuingEducation.setIssuingAuthority(updatedContinuingEducation.getIssuingAuthority());
        existingContinuingEducation.setStartDate(updatedContinuingEducation.getStartDate());
        existingContinuingEducation.setEndDate(updatedContinuingEducation.getEndDate());
        existingContinuingEducation.setHours(updatedContinuingEducation.getHours());
        existingContinuingEducation.setCredits(updatedContinuingEducation.getCredits());
        existingContinuingEducation.setRemarks(updatedContinuingEducation.getRemarks());
        existingContinuingEducation.setUpdatedAt(LocalDateTime.now());

        return continuingEducationRepository.save(existingContinuingEducation);
    }

    public void deleteContinuingEducation(Long id) {
        continuingEducationRepository.deleteById(id);
    }
}

